package com.wwz.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wwz.pojo.House;
import com.wwz.pojo.query.HouseQuery;
import com.wwz.service.IHouseService;
import com.wwz.util.EcoBootException;
import com.wwz.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 房产信息控制层
 * @date 2022/03/24 26:02:48
 */
@RestController
@RequestMapping("/manage/house")
@Api(tags = "房产信息控制层")
public class HouseController {

    @Resource
    private IHouseService houseService;

    @ApiOperation("房产列表")
    @GetMapping("/toPage")
    public Result toPage(HouseQuery query){
        IPage<House> houseIPage = houseService.toPage(query);
        return Result.ok(houseIPage);
    }

    @ApiOperation("添加或修改房产信息")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody House house){

        if (StrUtil.isAllEmpty(
                house.getHouseAddress(),
                house.getPrice(),
                house.getMaster())){
            throw new EcoBootException("请输入正确信息");
        }
        houseService.addOrUpdate(house);
        return Result.ok("ok");
    }

    @ApiOperation("删除房产信息")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        houseService.removeById(id);
        return Result.ok("ok");
    }

    @ApiOperation("查询所有")
    @GetMapping("allHouse")
    public Result getHouseList(){
        List<House> houses = houseService.list();
        return Result.ok(houses);
    }
}
